package br.edu.infnet.avaliacao.model;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "usuario", catalog = "avaliacao")
public class Usuario implements java.io.Serializable {

	private static final long serialVersionUID = -899559818604756529L;
	private String username;
	private String senha;
	private Set<Professor> professors = new HashSet<Professor>(0);
	private Set<Aluno> alunos = new HashSet<Aluno>(0);
	private Set<Papel> papels = new HashSet<Papel>(0);
	private transient boolean administrador , aluno , professor;
	private transient int totalAlunos;

	public Usuario() {
	}

	public Usuario(String username, String senha) {
		this.username = username;
		this.senha = senha;
	}

	public Usuario(String username, String senha, Set<Professor> professors,
			Set<Aluno> alunos, Set<Papel> papels) {
		this.username = username;
		this.senha = senha;
		this.professors = professors;
		this.alunos = alunos;
		this.papels = papels;
	}

	@Id
	@Column(name = "username", nullable = false, length = 50)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "senha", nullable = false, length = 50)
	public String getSenha() {
		return this.senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "usuario")
	public Set<Professor> getProfessors() {
		return this.professors;
	}

	public void setProfessors(Set<Professor> professors) {
		this.professors = professors;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "usuario")
	public Set<Aluno> getAlunos() {
		return this.alunos;
	}

	public void setAlunos(Set<Aluno> alunos) {
		this.alunos = alunos;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "usuario_papel", catalog = "avaliacao", joinColumns = { @JoinColumn(name = "username", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "papel", nullable = false, updatable = false) })
	public Set<Papel> getPapels() {
		return this.papels;
	}

	public void setPapels(Set<Papel> papels) {
		this.papels = papels;
	}
	
	@Transient
	public boolean isAdministrador(){
		for(Papel papel:this.getPapels()){
			if(papel.getPapel().equals("admin")){
				return true;
			}
		}
		return false;
	}
	
	@Transient
	public boolean isAluno(){
		for(Papel papel:this.getPapels()){
			if(papel.getPapel().equals("aluno")){
				return true;
			}
		}
		return false;
	}
	
	@Transient
	public boolean isProfessor(){
		for(Papel papel:this.getPapels()){
			if(papel.getPapel().equals("professor")){
				return true;
			}
		}
		return false;
	}
	
	@Transient
	public int getTotalAlunos(){
		return this.alunos.size();
	}

}
